<?php

namespace app\beautiful\model\finance;

use app\common\model\account\AccountModel;
use app\common\model\admin\AdminModel;
use app\common\model\CommonModel;
use think\facade\Db;


class WithdrawModel extends CommonModel{

    protected  $name = 'beautiful_withdraw';

    protected  $pk   = 'withdraw_id';


    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function getById($where,$field='*')
    {
        $list = Db::name('beautiful_withdraw')->where($where)->field($field)->find();
        return $list;
    }
    public static function info($where,$field='*')
    {
        $list = Db::name('beautiful_withdraw')->alias('w')
            ->leftjoin('beautiful_withdraw_account wa','wa.withdraw_account_id=w.withdraw_account_id')
            ->where(['w.is_delete'=>0])
            ->when(!empty($where['withdraw_id']),function ($query) use ($where){
                $query->where(['w.withdraw_id'=>$where['withdraw_id']]);
            })->field($field)->find();
        return $list;
    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_withdraw')->alias('w')
            ->join('shop s','s.shop_id=w.shop_id')
            ->join('shop_detail sd','sd.shop_id=w.shop_id')
            ->join('beautiful_withdraw_account wa','wa.withdraw_account_id=w.withdraw_account_id')
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->whereIn('w.status',$where['status']);
            })->when(!empty($where['name']),function ($query) use ($where){
                $query->where('s.name','like','%'.$where['name'].'%');
            })->when(!empty($where['is_delete']),function ($query) use ($where){
                $query->where('w.is_delete','=',$where['is_delete']);
            })->when(!empty($where['type']),function ($query) use ($where){
                $query->where('w.type','=',$where['type']);
            })->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where('w.shop_id','=',$where['shop_id']);
            })->when(!empty($where['start_time']),function ($query) use ($where){
                $query->where('w.add_time','>=',$where['start_time']);
            })->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('w.add_time','<=',$where['end_time']);
            })
            ->field('w.remarks,w.operate_id,w.withdraw_id,w.status,w.money,w.fee,w.before_money-w.after_money-w.fee as received_money,w.pay_time,w.pay_voucher,w.add_time,wa.type as settlement_type,wa.open_bank_name,wa.open_branch,wa.account_name,
            wa.account_number,s.name as shop_name,sd.logo as shop_logo');
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();

        if($list){
            foreach ($list as $k=>$v){
                if($v['status']==1) $list[$k]['settlement_type'] = $v['pay_voucher'];
                $list[$k]['settlement_price'] = $v['money'];
                $list[$k]['settlement_time'] = $v['status']==1?date('Y-m-d',strtotime($v['pay_time'])):null;
                $admin= AdminModel::find($v['operate_id']);
                $list[$k]['name'] = $admin?$admin['username']:null;

            }
        }
        return compact('count','list');
    }

}